<template>
  <el-row :gutter="24" class="pl80">
    <el-col>
      <h2>
        拒收或上交礼金、礼品情况
        <el-tooltip class="item mr10" effect="dark" content="新增" placement="top">
          <el-button
            size="small"
            class="fr"
            type="primary"
            icon="el-icon-plus"
            @click="handleEdit(false, {})"
          />
        </el-tooltip>
        <el-tooltip class="item mr10" effect="dark" content="打印" placement="top">
          <el-button size="small" class="fr" icon="el-icon-s-open" @click="handlePrint" />
        </el-tooltip>
      </h2>
    </el-col>
    <el-col>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="disposeDate" label="拒收或上交时间(年月)" width="150"></el-table-column>
        <el-table-column align="center" label="拒收或上交礼品情况">
          <el-table-column align="center" prop="giftName" label="名称"></el-table-column>
          <el-table-column align="center" prop="giftNum" label="数量"></el-table-column>
          <el-table-column align="center" prop="giver" label="送礼方"></el-table-column>
          <el-table-column align="center" prop="totalRmb" label="折合人民币(元)"></el-table-column>
          <el-table-column align="center" prop="disposeType" label="礼品处理类型">
            <template slot-scope="scope">{{ scope.row.disposeType === 0 ? '拒收' : '上交' }}</template>
          </el-table-column>
        </el-table-column>
        <el-table-column prop="remark" align="center" label="备注"></el-table-column>
        <el-table-column name="action" fixed="right" width="50" align="center">
          <template slot-scope="scope">
            <el-dropdown>
              <a class="el-dropdown-link">
                <i class="el-icon-menu"></i>
              </a>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="handleEdit(true, scope.row)">
                  <a>修改</a>
                </el-dropdown-item>
                <el-dropdown-item @click.native="handleDelete(scope.row)">
                  <a class="red">删除</a>
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
    </el-col>
    <gift-modal ref="giftModal" @refresh="handleRefresh" />
  </el-row>
</template>

<script>
import { getUserGift, deleteUserGift } from '@/api/archives'
import { setSession, getSession, serviceParam } from '@/utils/util'
import GiftModal from './GiftModal'

export default {
  components: {
    GiftModal
  },
  data() {
    return {
      loading: false,
      tableData: [],
      columns: [
        {
          label: '礼品名称',
          prop: 'giftName'
        },
        {
          label: '礼品数量',
          prop: 'giftNum'
        },
        {
          label: '送礼方',
          prop: 'giver'
        },
        {
          label: '礼品总金额',
          prop: 'totalRmb'
        },
        {
          label: '礼金',
          prop: 'cashGift'
        },
        {
          label: '礼品处理类型',
          prop: 'disposeType',
          formattter(row, column, cellValue, index) {
            return cellValue === 0 ? '拒收' : '上交'
          }
        },
        {
          label: '备注',
          prop: 'remark'
        },
        { slot: 'action' }
      ],
      leaderRow: JSON.parse(this.$route.query.leaderRow)
    }
  },
  methods: {
    handlePrint() {
      let obj = serviceParam('serverUrl')
      let uri = process.env.NODE_ENV === 'production' ? obj.paraValue : 'http://192.168.14.2:8888/'
      window.open(uri + 'ureport/preview?_u=2:user_gift_dispose&userId=' + this.leaderRow.userId)
    },
    handleRefresh() {
      this.handleGetList(this.leaderRow.userId)
    },
    handleShowHistory() {
      this.$emit('showHistory', 'family')
    },
    handleEdit(bool, row) {
      this.$nextTick(() => {
        this.$refs.giftModal.edit(bool, row)
      })
    },
    handleDelete(row) {
      const _this = this
      this.$confirm({
        title: '删除操作',
        content: `您确定删除这条记录吗？`,
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        centered: true,
        async onOk() {
          // 调用删除接口，刷新表格数据
          const res = await deleteUserGift(row.userGiftId, {})
          openNotification('success', '删除成功')
          _this.handleGetList(_this.leaderRow.userId)
        }
      })
    },
    async handleGetList(id, param = {}) {
      this.loading = true
      const res = await getUserGift(id, param)
      if (res && res.data.length > 0) {
        const { state, data } = res
        this.tableData = data
      } else {
        this.tableData = []
      }
      this.loading = false
    }
  },
  created() {
    if (this.leaderRow) {
      setSession('leaderRow', this.leaderRow)
    } else {
      this.leaderRow = getSession('leaderRow')
    }
    this.handleGetList(this.leaderRow.userId)
  }
}
</script>
